package cn.lanqiao.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.lanqiao.dao.UserLoginDao;
import cn.lanqiao.entity.UserLogin;
import cn.lanqiao.util.JdbcUtil;

public class UserLoginDaoImpl implements UserLoginDao {

	@Override
	public UserLogin getUser(String userAccount) {
		Connection conn = JdbcUtil.getConnection();
		String sql = "select * from USERS where account= ?";
		PreparedStatement ps = null;
		ResultSet rs= null;
		UserLogin user = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userAccount);
			rs = ps.executeQuery();
			if(rs.next()) {
			user = new UserLogin(rs.getString("userId"),rs.getString("userName"),rs.getString("account"),rs.getString("password"));
			}
			} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtil.close(rs, ps, conn);
		}
		return user;
	}

	@Override
	public int deleteUser(String userAccount) {
		Connection conn = JdbcUtil.getConnection();
		String sql = "delete users where account = ?";
		PreparedStatement ps = null;
		int affter = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userAccount);
			affter = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(null, ps, conn);
		}
		return affter;
	}
}
